package com.rain.shoppingscienceproj.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rain.shoppingscienceproj.R;
import com.rain.shoppingscienceproj.module.CarData;
import com.rain.shoppingscienceproj.module.OrderBetweenDto;
import com.rain.shoppingscienceproj.module.ShoppingcarDto;
import com.rain.shoppingscienceproj.ui.activity.order.OrderDetailsActivity;
import com.rain.shoppingscienceproj.utils.ImageUtils;
import com.rain.shoppingscienceproj.utils.layoutmanager.CustomLinearLayoutManager;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: Rain
 * Create: 2018/9/3 0:07
 */
public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.MyViewHolder> {

    private Context mContext;
    private List<OrderBetweenDto> list;
    DecimalFormat df = new DecimalFormat("#.00");
    private int type;//1用戶  2商戶

    private CancelButtonListener cancelButtonListener;

    private SubmitButtonListener submitButtonListeners;

    private ItemClickEvaluateListener itemClickEvaluateListener;

    public void setItemClickEvaluateListener(ItemClickEvaluateListener itemClickEvaluateListener) {
        this.itemClickEvaluateListener = itemClickEvaluateListener;
    }

    public void setCancelButtonListener(CancelButtonListener cancelButtonListener) {
        this.cancelButtonListener = cancelButtonListener;
    }

    public void setSubmitButtonListener(SubmitButtonListener submitButtonListener) {
        this.submitButtonListeners = submitButtonListener;
    }

    public void setList(List<OrderBetweenDto> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public OrderAdapter(Context mContext, int type) {
        this.mContext = mContext;
        this.type = type;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_item_make, parent, false);//解决宽度不能铺满
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        OrderBetweenDto myCarData = list.get(position);
        if (null != myCarData.getShop().getShopImage() || !"".equals(myCarData.getShop().getShopImage())) {
            ImageUtils.imageShop(mContext, myCarData.getShop().getShopImage(), holder.iv_store_image);
        }
//        double money = 0.00;
//        for (ShoppingcarDto goods :
//                myCarData.getShoppingcarDtos()) {
//            if (goods.getGoodsPay() != null && goods.getSize() != null) {
//                money += Double.valueOf(((goods.getGoodsPay() + goods.getGoodsKtpay()) / 100) + "") * goods.getSize();
//            }
//        }
        holder.tv_title.setText(myCarData.getShop().getShopName());
        CustomLinearLayoutManager linearLayoutManager = new CustomLinearLayoutManager(mContext);
        holder.rv_goods_order.setLayoutManager(linearLayoutManager);
        OrderGoodsAdapter orderGoodsAdapter = new OrderGoodsAdapter(mContext);
        holder.rv_goods_order.setAdapter(orderGoodsAdapter);
        orderGoodsAdapter.setList(myCarData.getShoppingcarDtos());
        holder.tv_order_num.setText("共计" + list.get(position).getShoppingcarDtos().size() + "件商品  小计:");
//        if (money < 1) {
//            holder.tv_money.setText(0 + df.format(money));
//        } else {
//        }\
        double money = Double.valueOf(myCarData.getTotalpay()) / 100;
        holder.tv_money.setText(df.format(money));
        holder.tv_cancel_order.setVisibility(View.VISIBLE);
        holder.ll_button.setVisibility(View.VISIBLE);
        if (type != 6) {
            orderGoodsAdapter.setType(myCarData.getStatus());
            orderGoodsAdapter.setmItemClickListener(shopId -> {
                Intent intent = new Intent(mContext, OrderDetailsActivity.class);
                intent.putExtra("orderId", myCarData.getOrderbetweenid());
                mContext.startActivity(intent);
            });
        }
        holder.ll_layout.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, OrderDetailsActivity.class);
            intent.putExtra("orderId", myCarData.getOrderbetweenid());
            mContext.startActivity(intent);
        });
        switch (myCarData.getStatus()) {
            case 0:
                holder.tv_cancel_order.setText("取消订单");
                holder.tv_submit_order.setText("立即支付");
                holder.tv_cancel_order.setOnClickListener(view -> cancelButtonListener.CancelButtonClickListener(1, myCarData.getOrderbetweenid()));
                holder.tv_submit_order.setOnClickListener(view -> submitButtonListeners.submitButtonListener(1, myCarData.getOrderbetweenid(), myCarData.getTotalpay() + ""));
                break;
            case 1:
                holder.ll_button.setVisibility(View.GONE);
                break;
            case 2:
                holder.tv_cancel_order.setText("查看物流");
                holder.tv_submit_order.setText("确认收货");
                holder.tv_cancel_order.setOnClickListener(view -> cancelButtonListener.CancelButtonClickListener(3, myCarData.getOrderbetweenid()));
                holder.tv_submit_order.setOnClickListener(view -> submitButtonListeners.submitButtonListener(3, myCarData.getOrderbetweenid(), myCarData.getShopid()));
                break;
            case 3:
                orderGoodsAdapter.setItemClickEvaluateListener(goodsId -> {
                    itemClickEvaluateListener.itemClickEvaluateListener(goodsId);
                });
                holder.ll_button.setVisibility(View.GONE);
//                holder.tv_submit_order.setOnClickListener(view -> submitButtonListeners.submitButtonListener(4, myCarData.getOrderbetweenid(), myCarData.getShopid()));
                break;
            default:
                holder.ll_button.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        } else {
            return list.size();
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.ll_layout)
        LinearLayout ll_layout;

        @BindView(R.id.iv_store_image)
        ImageView iv_store_image;

        @BindView(R.id.tv_title)
        TextView tv_title;

        @BindView(R.id.rv_goods_order)
        RecyclerView rv_goods_order;

        @BindView(R.id.tv_order_num)
        TextView tv_order_num;

        @BindView(R.id.tv_money)
        TextView tv_money;

        @BindView(R.id.ll_button)
        LinearLayout ll_button;

        @BindView(R.id.tv_cancel_order)
        TextView tv_cancel_order;

        @BindView(R.id.tv_submit_order)
        TextView tv_submit_order;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface ItemClickListener {
        void ItemClickListener(String orderId);
    }

    public interface CancelButtonListener {
        void CancelButtonClickListener(int type, String orderId);
    }

    public interface SubmitButtonListener {
        void submitButtonListener(int type, String orderId, String shopId);
    }

    public interface ItemClickEvaluateListener {
        void itemClickEvaluateListener(String goodsId);
    }
}
